import { BaseService, CoolCommException } from "@cool-midway/core";
import { DistributorInfoEntity } from "../entity/info";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { Repository } from "typeorm";
import { Provide } from "@midwayjs/core";
import { CommonAuditStatus } from "../../../interface";
import { AuditDistributorInfoDTO } from "../dto/info";

@Provide()
export class DistributorInfoService extends BaseService {
    @InjectEntityModel(DistributorInfoEntity)
    distributorInfoEntity: Repository<DistributorInfoEntity>

    // 商家端： 审核分销商信息
    async auditDistributor(dto: AuditDistributorInfoDTO) {
        const { id, status, rejectReason } = dto;
        const distributor = await this.distributorInfoEntity.findOne({ where: { id } });
        if (!distributor) {
            throw new CoolCommException('分销商不存在');
        }
        if (distributor.status !== CommonAuditStatus.PENDING) {
            throw new CoolCommException('分销商状态不正确');
        }
        return this.distributorInfoEntity.update(id, { status, rejectReason });
    }
}